﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Raider.WinForms.Actions.Properties;
using Raider.WinForms.Actions.Utils;

namespace Raider.WinForms.Actions.Standard
{
    /// <summary>
    /// Undoes the last action made ​​by the user.
    /// </summary>
    /// <remarks>
    /// Supports all controls derived from <see cref="TextBoxBase"/>.
    /// </remarks>
    [Description("Undoes the last action made by the user.\nSupports all controls derived from TextBoxBase.")]
    public class UndoAction : EditAction
    {
        public UndoAction()
            : base(Resources.Undo, Keys.Control | Keys.Z, Resources.UndoImage)
        {
        }

        protected override void OnExecute(ExecuteEventArgs args)
        {
            base.OnExecute(args);

            var textBox = lastFocusedControl as TextBoxBase;
            if (textBox != null) textBox.Undo();
        }

        protected override void OnUpdate(UpdateEventArgs args)
        {
            base.OnUpdate(args);

            var lastTextBox = lastFocusedControl as TextBoxBase;
            Enabled = lastTextBox != null && lastTextBox.CanUndo;
        }
    }
}
